package com.yl.zqy.controller.sys;

import com.yl.zqy.common.CustomException;
import com.yl.zqy.common.Result;
import com.yl.zqy.constant.ZQYConstant;
import com.yl.zqy.entity.sys.SysConfig;
import com.yl.zqy.enums.ResultCode;
import com.yl.zqy.service.sys.SysConfigService;
import com.yl.zqy.vo.common.IPageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 参数管理 前端控制器
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-24
 */
@RestController
@RequestMapping("/sysConfig")
@Tag(name = "系统参数管理", description = "SysConfigController")
@RequiredArgsConstructor
public class SysConfigController {
    private final SysConfigService configService;

    @PostMapping("/add")
    @Operation(summary = "新增系统参数", tags = "系统参数管理")
    public Result<Boolean> insertSysConfig(@RequestBody SysConfig config) {
        Optional.ofNullable(config.getParamCode()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "参数编码不能为空！"));
        Optional.ofNullable(config.getParamValue()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "参数值不能为空！"));
        return configService.insertSysConfig(config);
    }

    @PutMapping("/update")
    @Operation(summary = "修改系统参数", tags = "系统参数管理")
    public Result<Boolean> updateSysConfig(@RequestBody SysConfig config) {
        Optional.ofNullable(config.getId()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "id不能为空！"));
        Optional.ofNullable(config.getParamCode()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "参数编码不能为空！"));
        Optional.ofNullable(config.getParamValue()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "参数值不能为空！"));
        return configService.updateSysConfig(config);
    }

    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除系统参数", tags = "系统参数管理")
    public Result<Boolean> updateSysConfig(
            @Parameter(description = "id") @PathVariable(value = "id") String id) {
        Optional.ofNullable(id).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "id不能为空！"));
        List<String> ids = new ArrayList<>();
        ids.add(id);
        return configService.deleteSysConfigByIds(ids);
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "批量删除系统参数", tags = "系统参数管理")
    public Result<Boolean> updateSysConfig(
            @Parameter(description = "ids集合") @RequestParam List<String> ids) {
        Optional.ofNullable(ids).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "id不能为空！"));
        return configService.deleteSysConfigByIds(ids);
    }

    @GetMapping("/getListByPage")
    @Operation(summary = "获取系统参数分页数据", tags = "系统参数管理")
    public Result<IPageVo<SysConfig>> getListByPage(
            @Parameter(description = "页号") @RequestParam(value = "current", defaultValue = ZQYConstant.CURRENT) int current,
            @Parameter(description = "页大小") @RequestParam(value = "size", defaultValue = ZQYConstant.SIZE) int size,
            @Parameter(description = "条件查询（名称及编码）") @RequestParam(value = "query", required = false) String query) {
        return configService.getListByPage(current, size, query);
    }
}
